package com.wqm.step.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wqm.step.common.utils.FastJsonUtils;
import com.wqm.step.pojo.Customer;
import com.wqm.step.pojo.Order;
import com.wqm.step.service.HibernateService;

/**
 * 使用hibernate的HQL语句示例
 * 
 * @author wangqinmin 
 * @好诗： 青山不改，绿水长流
 */
@Controller
public class HQLController {

	@Autowired
	private HibernateService hibernateService;

	@RequestMapping(value = "saveDataInDatabase.action", produces = { "text/html;charset=UTF-8" })
	@ResponseBody
	public String saveDataInDatabase() {
		Customer c = new Customer();
		c.setName("不改");
		hibernateService.saveDataInDatabase(c);
		return "呵呵";
	}

	/**
	 * 基本查询：查询所有客户及客户下的所有订单
	 */
	@RequestMapping(value = "findAllCustomer.action", produces = { "text/html;charset=UTF-8" })
	@ResponseBody
	public String findAllCustomer() {
		List<Customer> list = hibernateService.findAllCustomer();
		// 没有专门去研究过json工具类，这里也没有找到合适的json转换工具，这个转换不正确，意思意思吧
		String string = FastJsonUtils.toJSONString(list);
		return string;
	}

	/**
	 * 排序查询：查询所有的订单，并根据订单的价格排序
	 * 
	 * @return
	 */
	@RequestMapping(value = "findOrderSort.action", produces = { "text/html;charset=UTF-8" })
	@ResponseBody
	public String findOrderSort() {
		List<Order> list = hibernateService.findOrderSort();
		String string = FastJsonUtils.toJSONString(list);
		return string;
	}

	/**
	 * 条件查询 /多条件查询 (查询Order表)
	 * @return
	 */
	@RequestMapping(value = "findCondition.action", produces = { "text/html;charset=UTF-8" })
	@ResponseBody
	public String findCondition() {
		List<Order> list = hibernateService.findCondition();
		/*String string = FastJsonUtils.toJSONString(list);*/
		return list.toString();
	}
	
	/**
	 * 分页查询条件查询
	 * @return
	 */
	@RequestMapping(value = "findPage.action", produces = { "text/html;charset=UTF-8" })
	@ResponseBody
	public String findPage() {
		List<Order> list = hibernateService.findPage();
		/*String string = FastJsonUtils.toJSONString(list);*/
		return list.toString();
	}
	
	/**
	 * 分组统计查询  
	 * @return
	 */
	@RequestMapping(value = "findGroup.action", produces = { "text/html;charset=UTF-8" })
	@ResponseBody
	public String findGroup() {
		List<Order> list = hibernateService.findGroup();
		/*String string = FastJsonUtils.toJSONString(list);*/
		return list.toString();
	}
	
	/**
	 *  投影查询 ： 查询具体列 
	 *  	例： String hql = "from Customer"; 就是查询所有，但是如果要查询具体字段就用投影查询
	 *  	例：String hql = "select id,name from Customer";
	 */
	@RequestMapping(value = "findColumnData.action", produces = { "text/html;charset=UTF-8" })
	@ResponseBody
	public String findColumnData() {
		List<Customer> list = hibernateService.findColumnData();
		/*String string = FastJsonUtils.toJSONString(list);*/
		return list.toString();
	}
}
